package com.shqcjd.yunweiyanoperator.ui.activity.remote;

import android.Manifest;
import android.content.Context;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

import com.HiScene.HiDeviceCenterKit.core.HiDeviceKitEngine;
import com.HiScene.HiDeviceCenterKit.core.entity.OperationResult;
import com.HiScene.HiDeviceCenterKit.listener.HiDeviceListener;
import com.HiScene.HiDeviceCenterKit.listener.HiMediaDataListener;
import com.HiScene.HiDeviceCenterKit.listener.ListenerManager;
import com.orhanobut.logger.Logger;
import com.shqcjd.yunweiyanoperator.R;
import com.shqcjd.yunweiyanoperator.ui.base.BaseActivity;
import com.shqcjd.yunweiyanoperator.ui.base.BasePresenter;
import com.shqcjd.yunweiyanoperator.util.ToastUtils;
import com.tbruyelle.rxpermissions2.RxPermissions;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.functions.Consumer;

/**
 * 设备连接设置
 */
public class DeviceConnectSettingActivity extends BaseActivity implements HiDeviceListener, HiMediaDataListener {


    @BindView(R.id.btn_finish)
    ImageView btnFinish;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    private Context mContext;

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_device_connect_setting;
    }

    @Override
    public void initView() {
        mContext = this;
        tvTitle.setText("设备设置");
        HiDeviceKitEngine.Instance().init();
        checkPermissions();
    }

    private void checkPermissions() {
        RxPermissions permissions = new RxPermissions(this);
        permissions.setLogging(true);
        permissions.request(
                Manifest.permission.WRITE_EXTERNAL_STORAGE)
                .subscribe(new Consumer<Boolean>() {
                    @Override
                    public void accept(Boolean aBoolean) throws Exception {
                        if (aBoolean) {

                        } else {
                            ToastUtils.showShort(mContext, "缺少必要权限");
                            finish();
                        }
                    }
                });
    }

    @OnClick(R.id.btn_finish)
    public void onViewClicked() {
        finish();
    }

    @Override
    protected void onStop() {
        super.onStop();
        if(isFinishing()){
            HiDeviceKitEngine.Instance().release();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        HiDeviceKitEngine.Instance().start();
        ListenerManager.getInstance().addHiDeviceListener(this);
        ListenerManager.getInstance().removeHiMediaDataListener(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        HiDeviceKitEngine.Instance().stop();
        ListenerManager.getInstance().removeHiDeviceListener(this);
        ListenerManager.getInstance().addHiMediaDataListener(this);

    }


    @Override
    public void handleMessageDeviceListChanged() {

    }

    @Override
    public void handleMessageDeviceChanged(String s) {

    }

    @Override
    public void handleMessageFinished(OperationResult operationResult) {

    }

    @Override
    public void onH264Data(byte[] bytes) {

    }

    @Override
    public void onYuvData(byte[] bytes, int i, int i1) {
        Logger.i("视频帧:"+bytes);
    }

    @Override
    public void onPCMFrame(byte[] bytes) {

    }

    @Override
    public void onAACFrame(byte[] bytes) {

    }

    @Override
    public void loadVideoComplete() {

    }
}
